package com.moorror.cola.strategy.plugin.view.component;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.actionSystem.ex.ComboBoxAction;
import com.intellij.openapi.project.DumbAwareAction;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.Set;

/**
 * @Author jiyanghuang
 * @Date 2022/3/16 11:11 PM
 */
public class CSComboBox extends DefaultActionGroup {

    private String currentShow;

    private Set<String> allItems;

    private String shortName;

    private ComboBoxAction comboBoxAction;

    public CSComboBox(Set<String> allItems, String shortName) {
        this.allItems = allItems;
        this.shortName = shortName;
        getTemplatePresentation().setIcon(AllIcons.General.Settings);
        getTemplatePresentation().setText(shortName);
        getTemplatePresentation().setDescription(shortName);
        initOrUpdateComboBoxAction(allItems, shortName);
    }

    public void initOrUpdateComboBoxAction(Set<String> allItems, String shortName) {
        comboBoxAction = new ComboBoxAction() {
            @Override
            protected @NotNull
            DefaultActionGroup createPopupActionGroup(JComponent button) {
                DefaultActionGroup outputItems = new DefaultActionGroup();
                for (String item : allItems) {
                    outputItems.add(new ItemSelectAction(item, item));
                }
                return outputItems;
            }

            @Override
            public void update(@NotNull AnActionEvent e) {
                Presentation presentation = e.getPresentation();
                if (null == e.getProject()) {
                    return;
                }
                System.out.println("update -->" + currentShow);
                presentation.setText(currentShow);
            }

//            @Override
//            public @NotNull
//            JComponent createCustomComponent(@NotNull Presentation presentation, @NotNull String place) {
//                JPanel jPanel = new JPanel(new GridBagLayout());
//                jPanel.add(new JLabel(shortName), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, JBUI.insetsLeft(5), 0, 0));
//                jPanel.add(super.createCustomComponent(presentation, place), new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, JBUI.emptyInsets(), 0, 0));
//                return jPanel;
//            }
        };
        for (String item : allItems) {
            currentShow = item;
            break;
        }
        comboBoxAction.setPopupTitle("BizCode");
        add(comboBoxAction);
    }

    /**
     * 先删除再添加
     *
     * @param allItems
     */
    public void update(Set<String> allItems) {
        remove(comboBoxAction);
        initOrUpdateComboBoxAction(allItems, shortName);
    }

    public String currentItem(){
        return currentShow;
    }


    private class ItemSelectAction extends DumbAwareAction {

        private String itemName;

        public ItemSelectAction(String itemName, String message) {
            super(message);
            this.itemName = itemName;
        }

        @Override
        public void actionPerformed(@NotNull AnActionEvent e) {
            if (itemName.equals(currentShow)) {
                return;
            }
            currentShow = itemName;
            System.out.println("actionUpdate -->" + itemName);
        }
    }
}
